package com.caiju.health.service.impl;

import com.caiju.health.entity.PageResult;
import com.caiju.health.mapper.ICheckGroupMapper;
import com.caiju.health.mapper.ICheckItemMapper;
import com.caiju.health.pojo.CheckGroup;
import com.caiju.health.pojo.CheckItem;
import com.caiju.health.service.ICheackGroupService;
import com.caiju.health.service.ICheackItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements ICheackGroupService {
    @Resource
    private ICheckGroupMapper cheackGroupMapper;
    @Override
    public PageResult findCheckGroupByCondition(Integer currentPage, Integer pageSize, String queryString) {
        if (currentPage==null){
            currentPage = 1;
        }
        if (pageSize==null){
            pageSize = 5;
        }
        Page<Object> page = PageHelper.startPage(currentPage,pageSize);
//        queryString="null" ,误以为有值
        if(queryString.equals("null")){
            queryString = null;
        }else {
            queryString = "%" + queryString + "%";
        }
        List<CheckGroup> checkItemList = cheackGroupMapper.findCondition(queryString);
        PageResult pageResult = new PageResult(page.getTotal(),checkItemList);
        return  pageResult;
    }

//    回显
    @Override
    public CheckGroup findCheckGroupById(Integer id) {
        CheckGroup checkGroup = cheackGroupMapper.findCheckGroupById(id);
        return checkGroup;
    }



    @Override
    public void updateCheckGroup(CheckGroup checkGroup) {
        cheackGroupMapper.updateCheckGroup(checkGroup);
    }

    @Override
    public void deleteCheckGroup(Integer id) {
        cheackGroupMapper.updateCheckGroupByStatus(id);
    }

    @Override
    public void addCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {
        cheackGroupMapper.addCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();

        for (Integer checkItemId:checkItemIds){
           HashMap<String,Object> hashMap = new HashMap<>();
           hashMap.put("checkGroupId",checkGroupId);
           hashMap.put("checkItemId",checkItemId);
           cheackGroupMapper.addCheckGroupAndCheckItem(hashMap);

        }
    }

    @Override
    public Integer[] findCheckItemIdsById(Integer id) {
       Integer[] checkItemIds = cheackGroupMapper.findCheckItemIdsById(id);
        return checkItemIds;
    }

    @Override
    public void deleteCheckItemIdsById(Integer id) {
        cheackGroupMapper.deleteCheckItemIdsById(id);
    }

    @Override
    public void addCheckGroupbyId(Integer id, Integer[] checkItemIds) {

        for (Integer checkItemId:checkItemIds){
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("checkGroupId",id);
            hashMap.put("checkItemId",checkItemId);
            cheackGroupMapper.addCheckGroupAndCheckItem(hashMap);

        }
    }

    @Override
    public List<CheckGroup> findAllCheckGroup() {
        List<CheckGroup> checkGroupList =  cheackGroupMapper.findAllCheckGroup();
        return checkGroupList;
    }


}
